/*
   tsAPI Distribution
   tsImage Class

   TrueSpectra Inc, 2002
   Code by: Kevin Martin
*/

function tsImage() {
	this.DynImage = DynImage;
	this.DynImage(arguments[0]);
	if (arguments[1]) this.link = arguments[1];

	this.img_width = null;			//special add
	this.img_height = null;			//special add
	this.k_changing = 0;			//track change request # so can cancel if mutiple request happen before update
	this.delay = 20;			//special add
	this.delayMax = 15000;
	this.firstLoad = true;
};

tsImage.prototype = new DynImage;

tsImage.prototype.setHTML = function( html,noevt ) {
	this.html=html?html:'';
	if (is.platform=="mac") this.html+='\n';
	if (this.css==null) return;
	if (noevt!=false) this.invokeEvent("beforeload");
	if( this.link ) {
		html = '<A HREF="'+this.link+'">'+html+'</A>';
		this.html = html;
        };
	this._setHTML(html);
	if (noevt!=false) this.invokeEvent("load")
};

/*tsImage.prototype.getWidth = function() {
	if( is.ns )	return this.getImage().width;
	else		return this.w;
};

tsImage.prototype.getHeight = function() {
	if( is.ns )	return this.getImage().height;
	else		return this.h;
};*/

//****New functions for buffering the image updates
//****Note: Added variable img_width and img_height 
//          which define the size of the image.


tsImage.prototype.set_size = function(w,h) { 
	this.img_width = w; 
	this.img_height = h; 
};

tsImage.prototype.setImageSpecial = function( sImg ) {
	this.invokeEvent("loadSpecial");
//this.setAutoResize( true );
	this.setImage( new DynImage.getImage( getTSGif(DynAPI.librarypath+"tsapi/images/spacer.tsgif"),1,1) );
	this.setImage( new DynImage.getImage(sImg) );

	this.k_changing++;
	setTimeout(this+'.specialUpdate('+this.k_changing+','+this.delayMax+')',this.delay);
};

function isMacIE() {
	if( is.platform=="mac" && (is.ie5 || is.ie4 ) ) return true;
	else return false;
};

tsImage.prototype.specialUpdate = function(i,delayMax) {
	if( i < this.k_changing ) {
		this.firstLoad = false;
		return;
	} else if( delayMax >= 0 ) {
		if( !this.getImage().complete ) {
			setTimeout(this+'.specialUpdate('+i+','+(delayMax-this.delay)+')',this.delay);
			return;
		} else {
			if( this.img_width != null && this.img_height != null ) {
				if( !isMacIE() && ( (  this.firstLoad && this.getClip() == "0,0,0,0" ) || Math.abs(this.getImage().width-this.img_width) > 5 || Math.abs(this.getImage().height-this.img_height) > 5 ) ) {
					setTimeout(this+'.specialUpdate('+i+','+(delayMax-this.delay)+')',this.delay);
					return;
				};
			};
		};
	};

	//***************
	// this is a fix for NS4 where Clipping region is wrong on first load
	// not required for other browsers but it doesn't hurt.
	this.firstLoad = false;
	if( this.img_width != null && this.img_height != null ) this.setClip( new Array(0,this.img_width,this.img_height,0) );

	//***************
	//  if done in NS4 then a loading box appears
	if( !is.ns4 && this.img_width != null && this.img_height != null ) this.setSize(this.img_width,this.img_height);

	this.k_changing=0;
	this.invokeEvent("doneSpecial");
};

function getTSGif(s) {
  return s;
};